<script>
  // 页面加载完成后执行
  document.addEventListener('DOMContentLoaded', function () {
    // 获取URL中的student_id参数
    const urlParams = new URLSearchParams(window.location.search);
    const studentId = urlParams.get('student_id');

    if (studentId) {
      // 调用后端API获取学生信息
      fetch(`/api/profile?student_id=${studentId}`)
        .then(response => response.json())
        .then(data => {
          if (data.code === 0) {
            // 填充表单数据
            document.getElementById('studentId').value = data.data.student_id || '';
            document.getElementById('name').value = data.data.name || '';
            document.getElementById('gender').value = data.data.gender || '';
            document.getElementById('birthday').value = data.data.birthday || '';
            document.getElementById('idCard').value = data.data.id_card || '';
            document.getElementById('nation').value = data.data.nation || '';
            document.getElementById('politicalStatus').value = data.data.political_status || '';
            document.getElementById('education').value = data.data.education || '';
            document.getElementById('major').value = data.data.major || '';
            document.getElementById('enrollmentYear').value = data.data.enrollment_year || '';
            document.getElementById('graduationYear').value = data.data.graduation_year || '';
            document.getElementById('phone').value = data.data.phone || '';
            document.getElementById('email').value = data.data.email || '';
            document.getElementById('address').value = data.data.address || '';
            document.getElementById('emergencyContact').value = data.data.emergency_contact || '';
            document.getElementById('emergencyPhone').value = data.data.emergency_phone || '';
          } else {
            alert('获取个人信息失败：' + data.msg);
          }
        })
        .catch(error => {
          console.error('Error:', error);
          alert('获取个人信息失败，请稍后重试');
        });
    } else {
      alert('未找到学生ID');
    }
  });
</script>